<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>导入excel方式二位ajax提交</title> 
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.11.0.min.js"></script>
<link rel="stylesheet" href="${pageContext.request.contextPath}/resources/easyUI/easyui.css" type="text/css"></link>
<link rel="stylesheet" href="${pageContext.request.contextPath}/resources/easyUI/easyui-lang-zh_CN.js" type="text/css"></link>
<script type="text/javascript" src="${pageContext.request.contextPath}/resources/easyUI/jquery.easyui.min.js"></script>
   
</head> 
<body> 
    <form action="" enctype="multipart/form-data" id="uploadForm" class="easyui-form">  		
		<tr class='normal'>  
           <td class='labely'>导入文件：</td>  
           <td class='context' colspan='3'>  
               <input type="file" id="excelFile" name="excelFile" class="text">  
                   <input type="submit" value="导入" onclick="addExcel();" class="btnstyle"/>  
           </td>  
       </tr> 
    </form>  
</body> 
<script type="text/javascript">  
function addExcel(){
	var excelFile = $("#excelFile").val();
	 if($.trim(excelFile) == ""){  
            alert("请先选择上传文件");  
            return false;  
        }  
        //获取文件后缀名  
        var value = excelFile.split(".")[excelFile.split(".").length -1];         
          
        if(value != "xls" && value != "xlsx"){  
            alert("文件格式错误");  
            return false;  
        }      
        $.ajax({
        	url:'${pageContext.request.contextPath}/readAjaxExcel.jspx',
        	type:'post',
        	dataType:'json',
        	cache: false,
        	data: new FormData($('#uploadForm')[0]),
        	processData: false,
        	contentType: false,
        	beforeSend: function () {
        		$.messager.progress({
    	            title: '执行中',
    	            text: '努力导入中...',
    	            interval: 400,
    	        });
        	},
        	success:function(data){
        		$.messager.progress('close');  //关闭进度条
        		if (data.code == 0) {
        			clearFileInput();//清空
        			$.messager.alert('成功', data.message);	
		            } else {			            	
		            	$.messager.alert('错误', data.message);	 			            	
		                return false;
		            }
        	},
        	error:function(data){
        		$.messager.progress('close');  //关闭进度条
        		$.messager.alert('错误', data.message);	 			            	
                return false;
        	}
        }); 
        	  
} 

function clearFileInput() {
    var file = $("#excelFile");
    file.after(file.clone().val(""));
    file.remove();
}
	        
 
       
</script>  
</html>